//
//  ASMakeAnimeMainVC.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASMakeAnimeMainVC.h"
#import "ASMakeAvatarSelectView.h"
#import "ASMakeSucceedController.h"
#import "ASHomeApi.h"
#import "ASAlertView.h"
#import "ASServiceModel.h"

@interface ASMakeAnimeMainVC ()

@property (strong, nonatomic) UIImageView *mainImage;
@property (strong, nonatomic) UIImageView *titleImage;
@property (strong, nonatomic) UILabel *titleLab;
@property (strong, nonatomic) ASMakeAvatarSelectView *selectView;
@property (strong, nonatomic) UIButton *backBtn;
@property (strong, nonatomic) UIButton *makeBtn;
@property (strong, nonatomic) UILabel *bottomLab;
@property (assign, nonatomic) NSInteger makeNum;

@end

@implementation ASMakeAnimeMainVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    [self.view addSubview:self.mainImage];
    [self.view addSubview:self.titleImage];
    [self.view addSubview:self.titleLab];
    [self.view addSubview:self.selectView];
    [self.view addSubview:self.makeBtn];
    [self.view addSubview:self.bottomLab];
    [self.view addSubview:self.backBtn];
    [self createUserNumHasMake:NO];
    [self setAllViewLayout];
    // Do any additional setup after loading the view.
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.titleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(Height_NavBar + 20);
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.titleImage.mas_bottom).mas_offset(26);
    }];
    
    
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.titleLab.mas_bottom).mas_offset(30);
        make.size.mas_equalTo(CGSizeMake(260, 260));
    }];
    

    [self.makeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(225, 40));
        make.top.mas_equalTo(self.selectView.mas_bottom).mas_offset(40);
    }];
    
    [self.bottomLab mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.makeBtn.mas_bottom).mas_offset(16);
    }];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(Height_StatusBar);
    }];
}

- (UIImageView *)mainImage{
    if (!_mainImage) {
        _mainImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover_bg"]];
        _mainImage.contentMode = UIViewContentModeScaleAspectFill;
        _mainImage.clipsToBounds = YES;
    }
    return _mainImage;
}

- (UIImageView *)titleImage{
    if (!_titleImage) {
        _titleImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover_wz"]];
    }
    return _titleImage;
}

- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.font = [UIFont systemFontOfSize:13];
        _titleLab.textColor = [UIColor colorWithHexString:@"ffffff"];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.text = @"小主的正脸效果最佳哦";
    }
    return _titleLab;
}

- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton new];
        [_backBtn setImage:[UIImage imageNamed:@"icon_back_white"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UILabel *)bottomLab{
    if (!_bottomLab) {
        _bottomLab = [UILabel new];
        _bottomLab.font = [UIFont systemFontOfSize:13];
        _bottomLab.textColor = [UIColor colorWithHexString:@"cccccc"];
        _bottomLab.textAlignment = NSTextAlignmentCenter;
        _bottomLab.hidden = YES;
    }
    return _bottomLab;
}

- (UIButton *)makeBtn{
    if (!_makeBtn) {
        _makeBtn = [UIButton new];
        [_makeBtn setTitle:@"一键制作" forState:UIControlStateNormal];
        [_makeBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
        [_makeBtn setBackgroundColor:[UIColor colorWithHexString:@"#FF7785"]];
        _makeBtn.titleLabel.font = [UIFont systemFontOfSize:19];
        _makeBtn.layer.masksToBounds = YES;
        _makeBtn.layer.cornerRadius = 20;
        [_makeBtn addTarget:self action:@selector(clickMakeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _makeBtn;
}

- (ASMakeAvatarSelectView *)selectView{
    if (!_selectView) {
        _selectView = [ASMakeAvatarSelectView new];
        _selectView.layer.masksToBounds = YES;
        _selectView.layer.cornerRadius = 5.f;
        [_selectView setBackgroundColor:[UIColor colorWithHexString:@"#FFFFFF"]];
    }
    return _selectView;
}

- (void)clickMakeBtn{
    if (self.selectView.showImage) {
        if ([self createUserNumHasMake:YES]) {
//            [self pushSucceed:self.selectView.showImage];
            [ASHomeApi baiduAnimeImageWithImage:self.selectView.showImage loadingStr:@"制作中..." Completion:^(UIImage * _Nonnull image) {
                [self pushSucceed:image];
            } failure:^(NSString * _Nonnull errorStr) {
                [MBProgressHUD showError:errorStr];
            }];
        }else{
            [MBProgressHUD showError:@"请小主明天再来哦~"];
//            [self showAlert];
        }
    }else{
        [MBProgressHUD showError:@"请选择一张图片"];
    }
}

- (void)showAlert{
    ASAlertView *ale = [[ASAlertView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    ale.titleLab.textAlignment = NSTextAlignmentCenter;
    [ale setContentStr: @"当天免费次数已用完，请明天再试"];
    [win addSubview:ale];
    ale.clickSubBtnBlock = ^(NSInteger btnTag) {

    };
}

- (void)pushSucceed:(UIImage *)showImage{
    ASMakeSucceedController *makeVC = [ASMakeSucceedController new];
    makeVC.showImage = showImage;
    [self.navigationController pushViewController:makeVC animated:YES];
}

- (void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}

///计算当天使用次数
- (BOOL)createUserNumHasMake:(BOOL)hasMake{
    NSInteger serviceNum = [ASServiceModel sharedManger].usage_count.integerValue;
    if (serviceNum < 0) {
        return YES;
    }
    //一天只展示一次
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDate *now = [NSDate date];                            //当前时间
    NSDate *agoDate = [userDefault objectForKey:@"cropNowDate"];//上次展示时间
    NSNumber * makeNum = [userDefault objectForKey:@"AnimeMakeNum"];
    NSInteger makeNumInt = makeNum.integerValue;

    NSInteger timeCha = [ASTool numberOfDaysWithFromDate:agoDate toDate:now];

    if (timeCha >= 60 * 24 || !agoDate) {
        [userDefault setObject:now forKey:@"cropNowDate"];
        makeNumInt = serviceNum;
    }

    if (makeNumInt > 0) {
        if (hasMake) {
            makeNumInt --;
        }
        self.makeNum = makeNumInt;
        [userDefault setObject:[NSNumber numberWithInteger:makeNumInt] forKey:@"AnimeMakeNum"];
        [userDefault synchronize];

        return YES;
    }
    return NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
